package com.zzq.account.handler;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zzq.common.exception.BusinessException;
import com.zzq.common.exception.EmBusinessError;
import com.zzq.common.result.ResultData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

import static com.zzq.common.util.MsgUtil.getSubUtilSimple;

@Slf4j
@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = SQLException.class)
    public ResultData sqlErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        log.error(e.toString(), e);
        return ResultData.fail("请求出错");
    }

    @ResponseBody
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ResultData httpMessageNotReadableExceptionHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        log.error(e.toString(), e);
        if (StringUtils.isNotEmpty(e.getMessage())) {
            String errorMsg = "";
            String rawMessage = e.getMessage();
            log.info(rawMessage);
            String rgex = "Can not deserialize value of type[^\"]+\"(.*? not a valid[^;]+) value";

            //abcc": not a valid Integer
            String combinedMessage = getSubUtilSimple(rawMessage, rgex);
            int startIndex = combinedMessage.indexOf("\": not a valid ");
            if (startIndex > 0) {
                errorMsg += combinedMessage.substring(0, startIndex);

                if (combinedMessage.endsWith("Integer")) {
                    errorMsg += " 不是有效的数字";
                }
            } else {
                errorMsg = combinedMessage;
            }

            return ResultData.fail(errorMsg);
        }
        return ResultData.fail("请求出错");
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public ResultData defaultBusinessErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        if (e instanceof BusinessException) {
            BusinessException businessException = (BusinessException) e;
            return ResultData.fail(businessException.getErrCode(), businessException.getErrMsg());
        } else {
            return ResultData.fail(EmBusinessError.UNKNOWN_ERROR.getErrCode(), EmBusinessError.UNKNOWN_ERROR.getErrMsg());
        }
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultData defaultErrorHandler(HttpServletRequest req, HttpServletResponse resp, Exception e) {
        log.error(e.toString(), e);
        if (StringUtils.isNotEmpty(e.getMessage())) {
            return ResultData.fail(e.getMessage());
        }
        return ResultData.fail("请求出错");
    }

}
